package com.ruoyi.community.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.service.IOwnerActivityService;

/**
 * 活动列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-28
 */
@Service
public class OwnerActivityServiceImpl implements IOwnerActivityService 
{
    @Autowired
    private OwnerActivityMapper ownerActivityMapper;

    @Autowired
    private OwnerVoteMapper ownerVoteMapper;

    @Autowired
    private OwnerMemberVoteMapper ownerMemberVoteMapper;

    @Autowired
    private OwnerActivityEnrollMapper ownerActivityEnrollMapper;

    @Autowired
    private OwnerActivityParticipateMapper ownerActivityParticipateMapper;

    /**
     * 查询活动列表
     * 
     * @param activityId 活动列表ID
     * @return 活动列表
     */
    @Override
    public OwnerActivity selectOwnerActivityById(String activityId)
    {
        OwnerActivity resultOwnerActivity = ownerActivityMapper.selectOwnerActivityById(activityId);
        Long viewNumber = resultOwnerActivity.getViewNumber() + 1;
        resultOwnerActivity.setViewNumber(viewNumber);
        updateOwnerActivity(resultOwnerActivity);
        return resultOwnerActivity;
    }

    /**
     * 查询活动列表
     * 
     * @param ownerActivity 活动
     * @return 活动列表
     */
    @Override
    public List<OwnerActivity> selectOwnerActivityList(OwnerActivity ownerActivity)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ownerActivity.setCommunityId(Long.valueOf(loginUser.getCommunityId()));
        return ownerActivityMapper.selectOwnerActivityList(ownerActivity);
    }

    /**
     * 新增活动
     * 
     * @param ownerActivity 活动
     * @return 结果
     */
    @Override
    public int insertOwnerActivity(OwnerActivity ownerActivity)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ownerActivity.setCreater(loginUser.getUsername());
        ownerActivity.setCommunityId(Long.valueOf(loginUser.getCommunityId()));
        Long registerCode = Math.round((Math.random()+1) * 1000);
        ownerActivity.setRegisterCode(registerCode);
        return ownerActivityMapper.insertOwnerActivity(ownerActivity);
    }

    /**
     * 修改活动列表
     * 
     * @param ownerActivity 活动列表
     * @return 结果
     */
    @Override
    public int updateOwnerActivity(OwnerActivity ownerActivity)
    {
        return ownerActivityMapper.updateOwnerActivity(ownerActivity);
    }

    /**
     * 批量删除活动列表
     * 
     * @param activityIds 需要删除的活动列表ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityByIds(String[] activityIds)
    {
        return ownerActivityMapper.deleteOwnerActivityByIds(activityIds);
    }

    /**
     * 删除活动列表信息
     * 
     * @param activityId 活动列表ID
     * @return 结果
     */
    @Override
    public int deleteOwnerActivityById(String activityId)
    {
        return ownerActivityMapper.deleteOwnerActivityById(activityId);
    }

    @Override
    public List<OwnerMyRecord> getMyRecord()
    {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        // 用户id，时间，类型（投票0，活动报名1，活动参与2），投票或活动id，投票或活动名
//        List<OwnerMyRecord> list = new ArrayList<>();
        List<OwnerMyRecord> list = ownerActivityMapper.selectMyRecord(userId.toString());
//        OwnerMemberVote ownerMemberVote = new OwnerMemberVote();
//        ownerMemberVote.setUserId(userId);
//        List<OwnerMemberVote> voteRecordList = ownerMemberVoteMapper.selectOwnerMemberVoteList(ownerMemberVote);
//        OwnerMyRecord ownerMyRecord = new OwnerMyRecord();
//        ownerMyRecord.setUserId(userId);
//        for (OwnerMemberVote memberVote : voteRecordList) {
//            ownerMyRecord.setTime(memberVote.getSyncTime());
//            ownerMyRecord.setType(0);
//            String voteId = memberVote.getVoteId();
//            ownerMyRecord.setRecordActivityId(voteId);
//            ownerMyRecord.setTitle(ownerVoteMapper.selectOwnerVoteById(voteId).getTitle());
//            list.add(ownerMyRecord);
//        }
//
//        OwnerActivityEnroll ownerActivityEnroll = new OwnerActivityEnroll();
//        ownerActivityEnroll.setUserId(userId);
//        List<OwnerActivityEnroll> enrollRecordList = ownerActivityEnrollMapper.selectOwnerActivityEnrollList(ownerActivityEnroll);
//        for (OwnerActivityEnroll activityEnroll : enrollRecordList) {
//            ownerMyRecord.setTime(activityEnroll.getTime());
//            ownerMyRecord.setType(1);
//            String activityId = activityEnroll.getActivityId();
//            ownerMyRecord.setRecordActivityId(activityId);
//            ownerMyRecord.setTitle(ownerActivityMapper.selectOwnerActivityById(activityId).getTitle());
//            list.add(ownerMyRecord);
//        }
//
//        OwnerActivityParticipate ownerActivityParticipate = new OwnerActivityParticipate();
//        ownerActivityParticipate.setUserId(userId);
//        List<OwnerActivityParticipate> participateRecordList = ownerActivityParticipateMapper.selectOwnerActivityParticipateList(ownerActivityParticipate);
//        for (OwnerActivityParticipate activityParticipate : participateRecordList) {
//            ownerMyRecord.setTime(activityParticipate.getTime());
//            ownerMyRecord.setType(1);
//            String activityId = activityParticipate.getActivityId();
//            ownerMyRecord.setRecordActivityId(activityId);
//            ownerMyRecord.setTitle(ownerActivityMapper.selectOwnerActivityById(activityId).getTitle());
//            list.add(ownerMyRecord);
//        }

        return list;
    }
}
